/**
 * 
 */
package arisufascion.com.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import arisufascion.com.AbstractActivity;
import arisufascion.com.R;
import arisufascion.com.adapter.ListNewProductAdapter;
import arisufascion.com.compone.LoadMoreListView;
import arisufascion.com.compone.LoadMoreListView.OnLoadMoreListener;
import arisufascion.com.constant.Constants;
import arisufascion.com.handler.ProductHandler;
import arisufascion.com.handler.ServiceHandler;
import arisufascion.com.model.Product;
import arisufascion.com.network.RequestTask;
import arisufascion.com.tabs.TabGroupActivity;

/**
 * @author Kelvin
 *
 */
public class ActivityProducts extends AbstractActivity{
	
	private int page = 1;
	private LayoutInflater inflater;
	View footer;
	private boolean isLoading = true;
	private ProgressDialog dialog;
	private LoadMoreListView myproduct_list;
	private ListNewProductAdapter mAdapter;
	private List<Product> products=new ArrayList<Product>();
	private int categories_id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_products);
		
		findViewById(R.id.btn_menu).setOnClickListener(onClick);
		findViewById(R.id.btn_privacy).setOnClickListener(onClick);
		findViewById(R.id.btn_wholesale).setOnClickListener(onClick);
		findViewById(R.id.btn_shipping).setOnClickListener(onClick);
		
		inflater = LayoutInflater.from(this);
		footer = inflater.inflate(R.layout.loadmore, null);
		myproduct_list = (LoadMoreListView) findViewById(R.id.list_products);

		
		myproduct_list.addFooterView(footer);
		myproduct_list.setOnLoadMoreListener(loadMoreListener);
		myproduct_list.setOnItemClickListener(onItemClick);
		Bundle bundle=getIntent().getExtras();
		if(bundle!=null){
			categories_id=bundle.getInt(CATEGORIE_ID);
		}
		
		if (isNetworkAvailable()) {
			if (products.size() < 1) {
				
				new getProductsFromServer(page).execute();
			}
		} else {
			Toast.makeText(self, "Please On wifi or 3G.", Toast.LENGTH_LONG)
					.show();
			self.finish();
		}
		
	}
	
	OnLoadMoreListener loadMoreListener = new OnLoadMoreListener() {

		@Override
		public void onLoadMore() {
			page++;
			new getProductsFromServer(page).execute();
		}
	};
	
	
	public class getProductsFromServer extends AsyncTask<Void, Void, List<Product>> {

		int page = 1;

		public getProductsFromServer(int page) {
			this.page = page;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// Show Dialog
			if (isLoading) {
				dialog = new ProgressDialog(getParent());
				dialog.setMessage("Fetching data...");
				dialog.show();
				isLoading = false;
			}
		}

		@Override
		protected List<Product> doInBackground(Void... params) {

			String url = Constants.SERVER_URL_PRODUCTS;
			url = String.format(url, Constants.SERVER_API_KEY,categories_id, page);

			Log.d("get product by categorie Id: ", url);

			String json = RequestTask.makeServiceCall(url);
			
			Log.d("get product by categorie Id: ", json);
			
			
			ServiceHandler service = new ServiceHandler();
			ProductHandler mProductHandler = service.getProductLatests(json);
			
			return mProductHandler.getData();

		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(List<Product> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}

			if (result != null) {
				if (products == null || products.isEmpty()) {
					footer.setVisibility(VISIBLE);
					products = result;
					int size = result.size();

					if (size < 15) {
						footer.setVisibility(GONE);
						myproduct_list.setOnLoadMoreListener(null);
					}

					products = result;
					mAdapter = new ListNewProductAdapter(self, products);
					myproduct_list.setAdapter(mAdapter);

				} else {
					int size = result.size();
					if (size < 15) {
						footer.setVisibility(GONE);
						myproduct_list.setOnLoadMoreListener(null);
					}
					mAdapter.addItem(result);
					myproduct_list.onLoadMoreComplete();
				}
			} else {
				footer.setVisibility(GONE);
				myproduct_list.setOnLoadMoreListener(null);
			}
		}
	}
	
	OnItemClickListener onItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long arg3) {
			view.setSelected(true);

			Product pro = (Product) parent.getAdapter().getItem(position);
			if(pro!=null){

				Bundle bundle = new Bundle();
				bundle.putParcelable(PRODUCT, pro);
				Intent intent_product = new Intent(self, Activity_Detail.class);
				
				intent_product.putExtras(bundle);
//				startActivity(intent_product);
				
				TabGroupActivity tabGroupActivity = (TabGroupActivity) getParent();
				tabGroupActivity.startChildActivity("Activity_Detail",
						intent_product);
			}
		}
	};

}